/**
 * Created by zycool on 2016/9/8.
 * 此日历插件为 阴保计划 单独制作
 */

;(function($){
    var CL = function(element, options){
        this.element = element;
        this.tempDate = new Date();
        this.defaults = {
            nowDate: new Date()
        };
        this.settings = $.extend({},this.defaults,options);
    };

    CL.prototype = {
        // 获取当前日期
        getNowDate: function(){
            return new Date();
        },
        // 获取当前日期是周几
        getDay: function(date){
            var day = date.getDay();
            if(0 == day){
                day = 7;
            }
            return day;
        },
        // 获取某月份的天数
        getDays: function(date){
            var currentDate = date.getDate();
            var month = date.getMonth() + 1;
            date.setMonth(month,0);
            var days = date.getDate();
            date.setMonth(month-1,currentDate);
            return days;
        },
        // 当前月份的第一天是周几
        getFirstDayOfMonth: function(date){
            var self = this;
            var currentDate = date.getDate();
            date.setDate(1);
            var firstDayOfMonth = self.getDay(date);
            date.setDate(currentDate);
            return firstDayOfMonth;
        },
        // 当前月份的最后一天是周几
        getLastDayOfMonth: function(date){
            var self = this;
            var currentDate = date.getDate();
            var days = self.getDays(date);
            date.setDate(days);
            var lastDayOfMonth = self.getDay(date);
            date.setDate(currentDate);
            return lastDayOfMonth;
        },
        // 上月份结尾一行的日历
        getEndOfPreMonth: function(date){
            var self = this;
            var year = date.getFullYear();
            var month = date.getMonth();
            if (month == 0) {
                year -= 1;
                month = 11;
            }
            else {
                month -= 1;
            }
            var preMonth = new Date(year, month);
            var days = self.getDays(preMonth);
            var firstDayOfMonth = self.getFirstDayOfMonth(preMonth);
            var verbose = firstDayOfMonth;
            var row = '';
            for(var i = 1; i <= days; i++){
                row += '<td class="pre">'+i+'</td>';
                if ((i + verbose) % 7 == 0) {
                    row = '';
                }
            }
            return row;
        },
        // 下一个月份的前一行 或者前两行
        getHeadOfNextMonth: function(date){
            var self = this;
            var days = self.getDays(date);
            var firstDayOfMonth = self.getFirstDayOfMonth(date);
            var verbose = firstDayOfMonth;
            var row = '';
            var line = 0;
            for(var i = 1; i <= days; i++){
                if ((i + verbose) % 7 == 0) {
                    line++;
                }
            }
            //找出下一个月份
            var year = date.getFullYear();
            var month = date.getMonth();
            if (month == 11) {
                year += 1;
                month = 0;
            }
            else {
                month += 1;
            }
            var nextMonth = new Date(year, month);
            var firstDayOfNextMonth = self.getFirstDayOfMonth(nextMonth);
            verbose = firstDayOfNextMonth;
            //返回一行
            if (line == 5) {
                for (var j = 1; j <= 7 - verbose; j++) {
                    row += '<td class="next">' + j + '</td>';
                }
                row += '</tr>';
            }
            //返回两行
            if (line == 4) {
                for (var k = 1; k <= 7 - verbose; k++) {
                    row += '<td class="next">' + k + '</td>';
                }
                row += '</tr>';
                var secRow = "";
                for (; k <= 14 - verbose; k++) {
                    secRow += "<td class='next'>" + k + "</td>";
                    if ((k + verbose) % 7 == 0) {
                        row += "<tr>" + secRow + "</tr>";
                    }
                }
            }
            return row;
        },
        // 获得上一个月
        getPreMonth: function(){
            var self = this;
            var year = self.tempDate.getFullYear();
            var month = self.tempDate.getMonth();
            var date = self.tempDate.getDate();
            //改变年份和月份
            if (month == 0) {
                year -= 1;
                self.tempDate.setFullYear(year, 11, 1);
            }
            else {
                month -= 1;
                self.tempDate.setMonth(month, 1);
            }
            var preDays = self.getDays(self.tempDate);
            if (date >= preDays) {
                date = preDays;
            }
            self.tempDate.setDate(date);
            return self.tempDate;
        },
        // 获得上一个月
        getNextMonth: function(){
            var self = this;
            var year = self.tempDate.getFullYear();
            var month = self.tempDate.getMonth();
            var date = self.tempDate.getDate();
            //改变年份和月份
            if (month == 11) {
                year += 1;
                self.tempDate.setFullYear(year, 0, 1);
            }
            else {
                month += 1;
                self.tempDate.setMonth(month, 1);
            }
            var nextDays = self.getDays(self.tempDate);
            if (date >= nextDays) {
                date = nextDays;
            }
            self.tempDate.setDate(date);
            return self.tempDate;
        },
        // 初始化
        init: function(D){
            var self = this;
            self.element.empty();
            var $show = $('<div class="zy-calender-meta"></div>');
            var $show_year = $('<span class="zy-calender-year r"></span>');
            var $show_btn_box = $('<span class="zy-calender-btn-box r"></span>');
            var $show_pre = $('<i id="JQ_calender_pre" class="zy-calender-btn pre" title="上一月">‹</i>');
            var $show_next = $('<i id="JQ_calender_next" class="zy-calender-btn next" title="下一月">›</i>');
            var $show_now = $('<i id="JQ_calender_now" class="zy-calender-now hidden">今天</i>');

            // 翻上一月
            $show_pre.off('click').click(function(){
                var pre = self.getPreMonth();
                self.init(pre);
                var month = pre.getMonth() + 1;
                self.element.data('month',month);
                self.element.trigger('change',[month]);
            });

            // 翻下一月
            $show_next.off('click').click(function(){
                var next = self.getNextMonth();
                self.init(next);
                var month = next.getMonth() + 1;
                self.element.data('month',month);
                self.element.trigger('change',[month]);
            });

            // 今天
            $show_now.off('click').click(function(){
                var now = self.getNowDate();
                self.init(now);
                self.element.trigger('change',[now]);
            });

            // 当前号
            var date = D.getDate();
            // 当前年
            var year = D.getFullYear();
            // 当前月
            var month = D.getMonth()+1;

            $show_btn_box.append($show_now).append($show_pre).append($show_next);

            $show_year.text(year + '年' + month + '月');

            $show.append($show_btn_box).append($show_year);

            self.element.append($show);

            // 本月多少天
            var days = self.getDays(D);
            // 本月1号星期几
            var firstDayOfMonth = self.getFirstDayOfMonth(D);
            // 冗余变量 决定表格当中日期的第一行前端有几个空单元格
            var verbose = firstDayOfMonth;
            var dateString = '';
            dateString += '<table class="zy-calender" cellpadding="0" cellspacing="0"><thead><tr><th>星期天</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></tr></thead><tbody>';
            var row = self.getEndOfPreMonth(D);
            for(var i = 1; i <= days; i++){
                if(i == date){
                    row += '<td data-id="'+i+'" class="current today">'+month+'月'+i+'日</td>';
                }else{
                    row += '<td data-id="'+i+'" class="current">'+month+'月'+i+'日</td>';
                }
                // 到达周末后 换行
                if((i+verbose) % 7 == 0){
                    dateString += '<tr>'+row+'</tr>';
                    row = '';
                }
            }
            var endRow = self.getHeadOfNextMonth(D);
            dateString +=  '<tr>' + row + endRow + '</tbody></table>';

            self.element.append(dateString);
            self.element.data('month',month);
            self.element.data('year',year);

            return self.element;
        }
    };

    $.fn.CLD = function(options){
        var calender = new CL(this,options);
        return calender.init(new Date());
    };

})(jQuery);
